package com.thiny.android.game.thunderbolt.adapter;

import java.util.List;

import com.thiny.android.game.thunderbolt.R;
import com.thiny.android.game.thunderbolt.role.GamePlayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ScoreAdapter extends BaseAdapter {
	// 上下文
	private Context mContext;
	// 装载有GamePlayer对象的list
	private List<GamePlayer> mList;

	public ScoreAdapter(Context context, List<GamePlayer> list) {
		mContext = context;
		mList = list;
	}

	// 获取list大小
	@Override
	public int getCount() {
		if (mList == null) {
			return 0;
		}
		return mList.size();
	}

	// 获取list项
	@Override
	public Object getItem(int position) {
		if (mList == null || mList.isEmpty()) {
			return null;
		}
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// 获取listview每项的view
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (mList==null) {
			return null;
		}
		GamePlayer gamePlayer = mList.get(position);
		Holder holder = null;
		if (convertView == null) {
			holder = new Holder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.dialog_list_view_item, null);
			holder.num = (TextView) convertView.findViewById(R.id.item_num);
			holder.name = (TextView) convertView.findViewById(R.id.item_name);
			holder.score = (TextView) convertView.findViewById(R.id.item_score);
			holder.time = (TextView) convertView.findViewById(R.id.item_time);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		holder.num.setText((++position) + "");
		holder.name.setText(gamePlayer.getName());
		holder.score.setText(gamePlayer.getScore() + "");
		holder.time.setText(gamePlayer.getTime());
		return convertView;
	}

	static class Holder {
		TextView num;
		TextView name;
		TextView score;
		TextView time;
	}
}
